# -*- coding: utf-8 -*-

import sys
sys.path.append('../')

from PyQt4 import QtGui

from mainWindow import MainWindow
from compoundEdit  import CompoundEdit
from reactionEdit  import ReactionEdit
from referenceEdit import ReferenceEdit
from searchResults import SearchResults
from login import LoginDialog
from catalystEdit import CatalystEdit
import errorHandler

import database
import molsketch

class Controller:

	def __init__(self, app):

		self.app = app

		self.compoundEditFrame = CompoundEdit(self, database.Compound)

		self.reactionEditFrame = ReactionEdit(self, database.Reaction, database.Relation, database.Catalyst)
		self.referenceEditFrame = ReferenceEdit(self, database.Reference)
		self.searchResultsFrame = SearchResults(self)

		self.mainWindow = MainWindow(self)

		self.mainWindow.addEditFrames(self.compoundEditFrame, self.reactionEditFrame, self.referenceEditFrame)

		self.mainWindow.setEditFrame(self.compoundEditFrame)
		self.mainWindow.setSearchFrame(self.searchResultsFrame)


	def go(self):
		self.mainWindow.show()
		self.loginAction(None)
		database.dbHandle.setErrorHandler(errorHandler.myerrorhandler)

	def newCompoundAction(self):
		self.compoundEditFrame.clear()
		self.compoundEditFrame.setMode("New")
		self.setEditFrame(self.compoundEditFrame)

	def searchCompoundAction(self):
		self.compoundEditFrame.clear()
		self.compoundEditFrame.setMode("Search")
		self.setEditFrame(self.compoundEditFrame)

	def searchCompoundDialog(self, mode):
		self.searchResultsFrame.clear()

		if mode == "Reactant":
			self.searchResultsFrame.setMode("Adding Reactant")
		if mode == "Product":
			self.searchResultsFrame.setMode("Adding Product")

		self.dialog = QtGui.QDialog(self.mainWindow)
		self.dialog.frame = CompoundEdit(self, database.Compound, "Dialog")
		self.dialog.frame.setParent(self.dialog)
		self.dialog.frame.show()
		self.dialog.setModal(True)
		self.dialog.show()

	def catalystDialog(self, mode):
		self.searchResultsFrame.clear()
		self.searchResultsFrame.setMode("Adding Catalyst")

		self.dialog = QtGui.QDialog(self.mainWindow)
		self.dialog.frame = CatalystEdit(self, database.Catalyst, mode)
		self.dialog.frame.setParent(self.dialog)
		self.dialog.frame.show()
		self.dialog.setModal(True)
		self.dialog.show()

	def referenceDialog(self, mode):
		self.searchResultsFrame.clear()
		self.searchResultsFrame.setMode("Adding Reference")

		self.dialog = QtGui.QDialog(self.mainWindow)
		self.dialog.frame = ReferenceEdit(self, database.Reference, mode+" Dialog")
		self.dialog.frame.setParent(self.dialog)
		self.dialog.frame.show()
		self.dialog.setModal(True)
		self.dialog.show()

	def newReactionAction(self):
		self.reactionEditFrame.clear()
		self.setEditFrame(self.reactionEditFrame)

	def newReferenceAction(self):
		self.referenceEditFrame.clear()
		self.referenceEditFrame.setMode("New")
		self.setEditFrame(self.referenceEditFrame)

	def searchReferenceAction(self):
		self.referenceEditFrame.clear()
		self.referenceEditFrame.setMode("Search")
		self.setEditFrame(self.referenceEditFrame)

	def informationAction(self):
		self.mainWindow.viewEditFrame()

	def viewSearchAction(self):
		self.mainWindow.viewSearchFrame()

	def detailsAction(self):
		self.mainWindow.viewDetailsFrame()

	def setEditFrame(self, frame):
		self.mainWindow.setEditFrame(frame)

	def molsketchCallback(self, molfile):
		if molfile:
			self.compoundEditFrame.setStructure(molfile)

	def viewStructure(self):
		molfile = self.compoundEditFrame.getStructureText()
		if molfile:
			molsketch.run(self.molsketchCallback, str(molfile))
		else:
			molsketch.run(self.molsketchCallback)

	def createCompound(self, name, formula, iupac, inchi, structure):
		compound = database.Compound()

	def searchCompound(self, compound):
		compounds = database.searchForCompound(compound)
		self.searchResultsFrame.clear()
		self.searchResultsFrame.setMode("Compound")
		self.addCompoundsToSearch(compounds)

	def searchCompoundFromDialog(self, compound):
		compounds = database.searchForCompound(compound)
		if self.dialog:
			self.dialog.close()
		self.addCompoundsToSearch(compounds)

	def addCompoundsToSearch(self, compounds, message=""):
		if message:
			self.searchResultsFrame.clear()
			self.searchResultsFrame.setMode(message)
		self.searchResultsFrame.addCompounds(compounds)

	def searchReference(self, reference):
		references = database.searchForReference(reference)

		self.searchResultsFrame.clear()
		self.searchResultsFrame.setMode("Reference")
		self.searchResultsFrame.addReferences(references)

	def searchReferenceFromDialog(self, reference):
		references = database.searchForReference(reference)
		if self.dialog:
			self.dialog.close()

		self.searchResultsFrame.addReferences(references)

	def searchCatalyst(self, catalyst):
		catalysts = database.searchForCatalyst(catalyst)
		if self.dialog:
			self.dialog.close()
		self.searchResultsFrame.addCatalysts(catalysts)

	def setViewCurrentCompound(self, compound):
		self.compoundEditFrame.setCurrentCompound(compound, readonly=False)

	def setViewCurrentReference(self, reference):
		self.referenceEditFrame.setCurrentReference(reference, readonly=False)

	def addToReaction(self, obj, where):
		if self.dialog:
			self.dialog.close()
		print "Adding", obj.name, "to", where
		self.reactionEditFrame.addToList(obj, where)

	def loginAction(self, checked):
		self.login = LoginDialog()
		self.login.setModal(True)
		self.login.show()

	def logoutAction(self, checked):
		database.userUtils.logout()
